package com.real.operationalcontrols.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ToastUtils;
import com.google.gson.Gson;
import com.real.operationalcontrols.R;
import com.real.operationalcontrols.rxhttp.NetworkUrl;
import com.real.operationalcontrols.基类.TaskBaseFragment;
import com.real.operationalcontrols.实体类.WaringListBean;
import com.real.operationalcontrols.适配器.TaskYesAdapter;
import com.rxjava.rxlife.RxLife;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import io.reactivex.functions.Consumer;
import rxhttp.wrapper.param.RxHttp;

public class FragmentTaskYes extends TaskBaseFragment {
    @BindView(R.id.rv)
    RecyclerView rv;
    @BindView(R.id.frash)
    SmartRefreshLayout frash;
    Unbinder unbinder;
    TaskYesAdapter adapter;
    List<WaringListBean.TasklistBean> data = new ArrayList<>();

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_taskyes_layout;
    }

    @Override
    protected void loadData() {
        initData();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        unbinder = ButterKnife.bind(this, rootView);
        initAdapter();
        initFresh();
        return rootView;
    }

    private void initFresh() {
        //刷新
        frash.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        initData();
                    }
                }, 300);
                refreshLayout.finishRefresh(true);//刷新完成
            }
        });
    }

    private void initData() {
        Map<String, String> map = new HashMap<>();
        map.put("username", NetworkUrl.USERNAME);
        map.put("Act", "2");
        RxHttp.get(NetworkUrl.TASK_LIST)
                .addAll(map)
                .asString()
                .as(RxLife.asOnMain(this))
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String response) throws Exception {
                        WaringListBean bean = new Gson().fromJson(response, WaringListBean.class);
                        if (bean.isSuccess()) {
                            data.clear();
                            data.addAll(bean.getTasklist());
                            adapter.setData(data);
                        } else {
                            ToastUtils.showShort(bean.getMessage());
                        }
                    }
                });
    }

    private void initAdapter() {
        adapter = new TaskYesAdapter(getmContext());
        LinearLayoutManager layoutManager = new LinearLayoutManager(getmContext());
        //设置布局管理器
        rv.setLayoutManager(layoutManager);
        rv.setAdapter(adapter);
        rv.addItemDecoration(new DividerItemDecoration(getmContext(),
                DividerItemDecoration.VERTICAL));
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
